using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using UpdaterManagement.Data;
using UpdaterManagement.Models;

namespace UpdaterManagement.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class TestDataController : ControllerBase
    {
        private readonly ApplicationDbContext _context;

        public TestDataController(ApplicationDbContext context)
        {
            _context = context;
        }

        [HttpPost("seed")]
        public async Task<IActionResult> SeedTestData()
        {
            var testProducts = new[]
            {
                new VersionRelease { ProductName = "支付前置机", Version = "1.0.0", Description = "初始版本", IsActive = true },
                new VersionRelease { ProductName = "支付前置机", Version = "1.1.0", Description = "功能更新", IsActive = true },
                new VersionRelease { ProductName = "核心系统", Version = "2.0.0", Description = "核心系统初始版本", IsActive = true },
                new VersionRelease { ProductName = "核心系统", Version = "2.1.0", Description = "核心系统更新", IsActive = true },
                new VersionRelease { ProductName = "风控系统", Version = "1.5.0", Description = "风控系统", IsActive = true },
                new VersionRelease { ProductName = "报表系统", Version = "3.0.0", Description = "报表系统", IsActive = true }
            };

            foreach (var product in testProducts)
            {
                var existing = await _context.VersionReleases
                    .FirstOrDefaultAsync(v => v.ProductName == product.ProductName && v.Version == product.Version);
                if (existing == null)
                {
                    _context.VersionReleases.Add(product);
                }
            }

            await _context.SaveChangesAsync();
            return Ok("测试数据已添加");
        }
    }
}